/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-09-13
 * Time:9:42
 */


class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

public class Solution {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode t3=new ListNode();
        ListNode t4=t3;
        int num=0;
        while(l1!=null||l2!=null)
        {
            ListNode t5=new ListNode();
            if(l1!=null&&l2!=null)
            {
                t4.val=l1.val+l2.val+num;
                if(l1.val+l2.val+num>10)
                    num=1;
                else num=0;
                t4.next=t5;
                t4=t5;
                l1=l1.next;
                l2=l2.next;
            }
            else if(l1!=null)
            {
                t4.val=l1.val+num;
                if(l1.val+num>10)
                    num=1;
                else num=0;
                t4.next=t5;
                t4=t5;
                l1=l1.next;
            }
            else{
                t4.val=l2.val+num;
                if(l2.val+num>10)
                    num=1;
                else num=0;
                t4.next=t5;
                t4=t5;
                l2=l2.next;
            }
        }
        return t3;
    }

    public static void main(String[] args) {
        ListNode tt=new ListNode(2);
        ListNode yy=tt;
        yy=yy.next;
        yy.val=4;
        yy=yy.next;
        yy.val=3;

        ListNode cc=new ListNode(5);
        yy=cc;
        yy=yy.next;
        yy.val=6;
        yy=yy.next;
        yy.val=4;
        addTwoNumbers(tt,cc);
    }
}
